package com.springboot.controller;

import com.springboot.dto.MemorandumShowView;
import com.springboot.model.AssistantSelfReminderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.MemorandumService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class Memorandum {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    MemorandumService memorandumService;

    /**
     * 助理查看备忘录
     */
    @CrossOrigin
    @RequestMapping(value = "/getRemindServlet")
    public @ResponseBody
    String assistantSeeMemorandum(@RequestParam("assistantTel")  String tel)
    {
        System.out.println("显示备忘录");
        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            if(userLoginForm==null) {
                return "空";
            }
            Integer id=userLoginForm.getUserId();
            System.out.println(id+"显示备忘录");
            ArrayList<MemorandumShowView> memorandumShowViews=new ArrayList<>();
            memorandumShowViews=memorandumService.showMemorandumById(id);
            System.out.println("显示备忘录over");
            if(memorandumShowViews.size()==0)
            {
                return "空";
            }
            else
            {
                return memorandumShowViews.toString();
            }

        }
        catch (Exception e)
        {
            return "False";
        }
    }

    /**
     * 助理删除某条备忘录
     */
    @CrossOrigin
    @RequestMapping(value = "/deleteSelfRemind")
    public @ResponseBody
    String assistantSeePatient(@RequestParam("selfRemindId")  Integer itemId)
    {
        System.out.println("删除备忘录");
        try{
            AssistantSelfReminderForm assistantSelfReminderForm=new AssistantSelfReminderForm();
            int a=memorandumService.deleteMemorandumById(itemId);
            return "删除成功";
        }

        catch (Exception e)
        {
            return "删除失败";
        }
    }

    /**
     * 助理修改某条备忘录
     */
    @CrossOrigin
    @RequestMapping(value = "/modifySelfRemind")
    public @ResponseBody
    String assistantSeePatient(@RequestParam("remindId")  Integer itemId,
                               @RequestParam("remindDetail") String content,
                               @RequestParam("remindContent") String title)
    {
        System.out.println("修改备忘录");
        try{
            AssistantSelfReminderForm assistantSelfReminderForm=new AssistantSelfReminderForm();
            assistantSelfReminderForm.setItemId(itemId);
            assistantSelfReminderForm.setMemorandumTitle(title);
            assistantSelfReminderForm.setMemorandumContent(content);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date now = new Date();
            String dateTemp = dateFormat.format( now );
            java.util.Date d= null;
            try
            {
                d = dateFormat.parse(dateTemp);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            java.sql.Date date = new java.sql.Date(d.getTime());
            assistantSelfReminderForm.setCreateTime(date);
            try {
                int a=memorandumService.updateMemorandumById(assistantSelfReminderForm);
                return  "更新成功";
            }
            catch (Exception e){
                return "memorandumService.updateMemorandumById错误";
            }

        }
        catch (Exception e)
        {
            return "Flase";
        }
    }

    /**
     * 助理添加某条备忘录
     */
    @CrossOrigin
    @RequestMapping(value = "/addSelfRemind")
    public @ResponseBody
    String assistantSeePatient(@RequestParam("assistantTel")  String tel,
                               @RequestParam("content") String content,
                               @RequestParam("title") String title)
    {
        System.out.println("添加备忘录");
        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            if (userLoginForm==null){
                return "false";
            }
            Integer id=userLoginForm.getUserId();
            Integer a=memorandumService.getMaxItemId(id);
            a++;
            System.out.println(id+" "+a+"添加备忘录");
            AssistantSelfReminderForm assistantSelfReminderForm=new AssistantSelfReminderForm();
            assistantSelfReminderForm.setItemId(a);
            assistantSelfReminderForm.setAssistantId(id);
            assistantSelfReminderForm.setMemorandumContent(content);
            assistantSelfReminderForm.setMemorandumTitle(title);
            try{
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date now = new Date();
                String dateTemp = dateFormat.format( now );
                java.util.Date d= null;
                try
                {
                    d = dateFormat.parse(dateTemp);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
                java.sql.Date date = new java.sql.Date(d.getTime());
                assistantSelfReminderForm.setCreateTime(date);
                System.out.println("日期插入成功");
            }
            catch (Exception e){
                System.out.println("日期插入失败");
            }
            int b=memorandumService.insertMemorandumById(assistantSelfReminderForm);
            return "添加成功";
        }
        catch (Exception e)
        {
            return "flase";
        }
    }

}
